package com.example.managemyself.ui.ai;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.managemyself.R;
import com.example.managemyself.data.model.AIConversation;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * AI历史记录适配器
 */
public class AIHistoryAdapter extends RecyclerView.Adapter<AIHistoryAdapter.ConversationViewHolder> {
    
    private List<AIConversation> conversations = new ArrayList<>();
    private OnConversationClickListener listener;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
    
    public interface OnConversationClickListener {
        void onConversationClick(AIConversation conversation);
        void onConversationLongClick(AIConversation conversation);
        void onStarClick(AIConversation conversation);
    }
    
    public AIHistoryAdapter(OnConversationClickListener listener) {
        this.listener = listener;
    }
    
    @NonNull
    @Override
    public ConversationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_ai_conversation, parent, false);
        return new ConversationViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ConversationViewHolder holder, int position) {
        AIConversation conversation = conversations.get(position);
        holder.bind(conversation);
    }
    
    @Override
    public int getItemCount() {
        return conversations.size();
    }
    
    public void updateConversations(List<AIConversation> newConversations) {
        this.conversations = newConversations;
        notifyDataSetChanged();
    }
    
    class ConversationViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTitle;
        private TextView tvSummary;
        private TextView tvTime;
        private TextView tvMessageCount;
        private TextView tvCategory;
        private ImageView ivStar;
        private ImageView ivCategoryIcon;
        
        public ConversationViewHolder(@NonNull View itemView) {
            super(itemView);
            
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvSummary = itemView.findViewById(R.id.tv_summary);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvMessageCount = itemView.findViewById(R.id.tv_message_count);
            tvCategory = itemView.findViewById(R.id.tv_category);
            ivStar = itemView.findViewById(R.id.iv_star);
            ivCategoryIcon = itemView.findViewById(R.id.iv_category_icon);
            
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onConversationClick(conversations.get(getAdapterPosition()));
                }
            });
            
            itemView.setOnLongClickListener(v -> {
                if (listener != null) {
                    listener.onConversationLongClick(conversations.get(getAdapterPosition()));
                }
                return true;
            });
            
            ivStar.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onStarClick(conversations.get(getAdapterPosition()));
                }
            });
        }
        
        public void bind(AIConversation conversation) {
            tvTitle.setText(conversation.getTitle());
            
            // 设置摘要或最后一条消息
            String summary = conversation.getSummary();
            if (summary == null || summary.isEmpty()) {
                summary = conversation.getLastMessage();
            }
            if (summary == null || summary.isEmpty()) {
                summary = "暂无内容";
            }
            tvSummary.setText(summary);
            
            // 设置时间
            tvTime.setText(dateFormat.format(conversation.getUpdatedAt()));
            
            // 设置消息数量
            tvMessageCount.setText(String.valueOf(conversation.getMessageCount()));
            
            // 设置分类
            tvCategory.setText(conversation.getCategory());
            
            // 设置收藏状态
            ivStar.setImageResource(conversation.isStarred() ? 
                R.drawable.ic_star_filled : R.drawable.ic_star_outline);
            
            // 设置分类图标
            setCategoryIcon(conversation.getCategory());
        }
        
        private void setCategoryIcon(String category) {
            int iconRes;
            switch (category) {
                case "任务":
                    iconRes = R.drawable.ic_tasks_black_24dp;
                    break;
                case "健康":
                    iconRes = R.drawable.ic_tasks_black_24dp; // 暂时使用任务图标
                    break;
                case "日记":
                    iconRes = R.drawable.ic_diary_black_24dp;
                    break;
                default:
                    iconRes = R.drawable.ic_assistant_black_24dp;
                    break;
            }
            ivCategoryIcon.setImageResource(iconRes);
        }
    }
} 